# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time        : 2020/11/23 15:51
@Author      : Albert Darren
@Contact     : 2563491540@qq.com
@File        : Runge_function_interpolations.py
@Version     : Version 1.0.0
@Description : TODO
@Created By  : PyCharm
"""

from scipy.interpolate import lagrange,CubicSpline
import numpy as np
import pylab as mpl
import matplotlib.pyplot as plt


def runge(x):  # 龙格函数
    return 1 / (1 + 25 * x ** 2)


def draw(x_: np.ndarray, f: np.ndarray,fn):
    # 构造Lagrange函数
    # interpolation_ploynomial = lagrange(x_, f)
    # 画出连续的Lagrange函数图形
    x_range = np.linspace(-1, 1, 100)
    y_range = [fn(i) for i in x_range]
    plt.plot(x_range, y_range, label='自然条件三次样条插值函数S(x)', color='green')
    # 画出连续的runge函数
    runge_range=[runge(i) for i in x_range]
    plt.plot(x_range, runge_range, label=r'f(x)=$\frac{1}{1+25x^{2}}$', color='yellow')
    # 画出插值结点散点图
    plt.scatter(x_node, f, label="插值结点", color="red")
    # plt.title("%s次拉格朗日插值法结果" % n)
    plt.title("三次样条插值S(x)")
    mpl.rcParams['font.sans-serif'] = ['SimHei']
    mpl.rcParams['axes.unicode_minus'] = False
    plt.legend(loc="upper right")
    plt.show()


if __name__ == '__main__':
    # 构造11个插值结点做10次拉格朗日插值多项式
    x_node = np.linspace(-1, 1, 21)
    # 对应插值结点的龙格函数值
    w = np.array([runge(i) for i in x_node])
    ploy = CubicSpline(x_node, w, bc_type='natural')
    draw(x_node, w, ploy)
